package com.fsc.comadapoc1.presentation.panels
{
	import com.fsc.comadapoc1.domain.PortfolioModel;
	import com.fsc.comadapoc1.entities.Fund;
	import com.fsc.comadapoc1.events.LoadPortfolioEvent;
	import com.fsc.comadapoc1.events.LoadPortfolioFundsEvent;
	import com.fsc.comadapoc1.events.PanelChangeEvent;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	[Event(name="loadPortfolioFunds", type="events.LoadPortfolioFundsEvent")]
	[Event(name="changeState", type="events.PanelChangeEvent")]
	[ManagedEvents(names="loadPortfolioFunds,changeState")]
	public class SubscriptionsPanelPM extends EventDispatcher
	{
		/** The landing state **/
		public const LANDING_STATE : String = "landingState";
		
		/** The searching state. **/
		public const SEARCHING_STATE : String = "searchingState";
		
		/** The results state. **/
		public const RESULTS_STATE : String = "resultsState";
		
		/** The no results state. **/
		public const NO_RESULTS_STATE : String = "noResultsState";
		
		/**
		 * Keeps track of search term
		 */ 
		private var searchTerm:String;
		
		private var _currentState:String = LANDING_STATE;
		
		[Bindable]
		[Inject]
		public var model:PortfolioModel;
		
		/**
		 * Get the current state of the presentation model.
		 */
		[Bindable]
		public function get currentState() : String
		{
			return _currentState;
		}
		
		/**
		 * Sets the current state of the presentation model.
		 */
		public function set currentState( state:String ) : void
		{
			_currentState = state;	
		}
		
		/**
		 * Dispatches search events for searchTerm
		 * Calls search
		 */ 		
		public function search(term:String):void{
			searchTerm = term;
			//if search term is not blank
			//if(searchTerm != ""){	}
			dispatchEvent(new LoadPortfolioFundsEvent(LoadPortfolioFundsEvent.LOAD_PORTFOLIO_FUNDS,searchTerm));
			dispatchEvent(new PanelChangeEvent(PanelChangeEvent.SEARCH_STATE));
			
		}
		
		public function subscriptionDatagrid_clickHandler(selectedObject:*):void{
			
			
			if(selectedObject is Object){
				
				trace("PortfolioPanelPM: Selection Detected");
				var fund:Fund = new Fund();
				fund.id = selectedObject.Id;
				fund.name = selectedObject.Name;
				fund.referenceCurrency = selectedObject.ReferenceCurrency;
				
				model.currentPortfolio.selectedFund = fund;
				dispatchEvent(new PanelChangeEvent(PanelChangeEvent.SUBSCRIBE_VIEW_STATE  ));
			}
			
		}
		
		public function back():void{
			dispatchEvent(new PanelChangeEvent(PanelChangeEvent.PORTFOLIO_VIEW_STATE));
		}
		
		[MessageHandler(selector="portfolioFundsLoadComplete")]
		public function onSearchResults(event:LoadPortfolioFundsEvent):void{
			trace("SearchPanelPM: Received ");
			
			if(model.currentPortfolio.funds.length > 0){
				currentState = RESULTS_STATE;
				trace("lots of funds results")
			}else{
				currentState = NO_RESULTS_STATE;
				trace("no fund results")
			}
		}
		
	}
}